package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory

import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
import android.content.Intent
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.hardware.input.InputManager
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.*
import android.view.inputmethod.InputMethodManager
import android.widget.PopupWindow
import com.linxiao.framework.fragment.BaseFragment
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.intent
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.FragmentAdvisoryBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DiagnoseDocumentsBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.LookDiagnoseActivity
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.SendDiagnoseActivity
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.userFill.FillDiagnoseOneActivity
import com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory.kaifang.KaiFangActivity
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_advisory.*
import org.jetbrains.anko.sdk25.coroutines.onClick




/**
 * Created by Administrator on 2017/10/23.
 * 患者——咨询fragment
 */
class AdvisoryFragment: BaseFragment() {

//    private var wzdId: String=""
    private var wzdId: String="31"//测试用。。。。。。。。。。。。。。。。
    private val requestCode=1

    private var softInput=false//软键盘是否隐藏
    val popView by lazy {
        toasts("初始化popView")
        LayoutInflater.from(context).inflate(R.layout.popview_common_words,null) }

    val binding by lazy { getDataBinding<FragmentAdvisoryBinding>(R.layout.fragment_advisory) }
    override fun onCreatRootView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return binding.root
    }
    override fun onInitView(savedInstanceState: Bundle?) {
        val inputManager = activity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imgBiaoQing.onClick {
            //当软键盘处于弹出状态，隐藏软键盘
            if (softInput&&inputManager.isActive){
                inputManager.hideSoftInputFromWindow(etAdvisory.windowToken,0)
                softInput=false
            }
                bottomPullView.showAndHide()
        }

        etAdvisory.apply {
            //输入框被触摸时并且表情view展开时，将表情view隐藏
            setOnTouchListener { _, _ ->
                softInput=true
                if (bottomPullView.isShow()){
                    bottomPullView.showAndHide()
                }
                false
            }
        }
        val bundle =Bundle()
        bundle.putString("user_id","1")//当前user_id为假数据
        val intent=Intent(activity,SendDiagnoseActivity::class.java)
        intent.putExtra("bundle",bundle)
        tvWenZhenDan.onClick { startActivityForResult(intent,requestCode,bundle) }

        tvBianZhengKaiFang.onClick { toasts("辩证开方")}
//        tvHuanJiao.onClick { toasts("患教")}
//        tvPicture.onClick { toasts("图片")}



        tvHuanJiao.onClick {
            if (wzdId != ""){
                Log.e("this","问诊单id===="+wzdId)
                intentUserFillActivity(wzdId)
            }
        }
        tvPicture.onClick {
            if (wzdId!=""){
                startActivity(Intent(activity,LookDiagnoseActivity::class.java).putExtra("id",wzdId))
            }
        }



        tvSuiFangFuZhen.onClick { toasts("随访复诊")}
        tvCommentWords.onClick { showPopWin()}

    }

    //常用语popupWindow
    private fun showPopWin(){
        val popWindow=PopupWindow(popView,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,true)
        popWindow.setBackgroundDrawable(ColorDrawable(0x00000000))
        popWindow.animationStyle=R.style.bottom_popupWindow_anim

        popWindow.setOnDismissListener {
            val params = activity.window.attributes
            params.alpha = 1f
            activity.window.attributes = params
        }
        val params = activity.window.attributes
        params.alpha = 0.9f
        activity.window.attributes = params

        popWindow.showAtLocation(bottomPullView,Gravity.BOTTOM,0,0)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode==1){
            wzdId=data!!.getStringExtra("wzdId")
        }
    }

    //测试用
    private fun intentUserFillActivity(str: String){

        observable {
            postApi<Request<DiagnoseDocumentsBean>>(
                    hashMapOf("action" to "diagnose_info",
                            "token" to DoctorInfo.token,
                            "id" to str),
                    Consumer {
                        it.dispose {
                            startActivity(Intent(activity,FillDiagnoseOneActivity::class.java).putExtra("bean",it))
                        }
                    }
            )
        }
    }

}
